/*
 * LoginFilter.java
 *
 * Created on 03/09/2007, 23:28:41
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tcc.gui.web.filter;

import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import tcc.domain.login.TCCSystemUser;


public class LoginFilter implements Filter {

    // The filter configuration object we are associated with.  If
    // this value is null, this filter instance is not currently
    // configured.
    private FilterConfig filterConfig = null;
    private Map<String, String> params = new HashMap<String, String>();
    private String LOGIN_PAGE;

    public void destroy() {
    }

    /**
     * Init method for this filter
     *
     */
    public void init(FilterConfig filterConfig) {
        this.filterConfig = filterConfig;
        LOGIN_PAGE = this.filterConfig.getInitParameter("LOGIN_PAGE");

        Enumeration pNames = filterConfig.getInitParameterNames();
        while (pNames.hasMoreElements()) {
            String key = (String) pNames.nextElement();
            params.put(key, this.filterConfig.getInitParameter(key));
        }
    }

    /**
     * Return the filter configuration object for this filter.
     */
    public FilterConfig getFilterConfig() {
        return this.filterConfig;
    }

    public LoginFilter() {
    }

    /**
     *
     * @param request The servlet request we are processing
     * @param result The servlet response we are creating
     * @param chain The filter chain we are processing
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet error occurs
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        try {
            if (params.containsValue(((HttpServletRequest) request).getRequestURI())) {
                chain.doFilter(request, response);
            } else {

                HttpSession session = ((HttpServletRequest) request).getSession(false);
                if (session != null) {
                    TCCSystemUser user = (TCCSystemUser) session.getAttribute("user");

                    if (user != null) {
                        chain.doFilter(request, response);
                    } else {
                        ((HttpServletResponse) response).sendRedirect(LOGIN_PAGE);
                    }
                } else {
                    if (session != null) {
                        session.invalidate();
                    }
                    ((HttpServletResponse) response).sendRedirect(LOGIN_PAGE);
                }
            }
        } catch (Throwable t) {
            throw new ServletException(t);
        }
    }

    /**
     * Return a String representation of this object.
     */
    @Override
    public String toString() {
        if (filterConfig == null) {
            return "LoginFilter()";
        }
        StringBuffer sb = new StringBuffer("LoginFilter(");
        sb.append(filterConfig);
        sb.append(")");
        return sb.toString();
    }
}